
#include "Win32Clock.h"
#include "Common/Errors.h"

#include <MMSystem.h>
#include <windows.h>


using namespace ttg::threads::win32;

//------------------
//--- Win32Clock ---
//------------------

Win32Clock::Win32Clock()
{
	if ((timeBeginPeriod(1)) != TIMERR_NOERROR)
		throw Error(Error::FATAL, "Win32Clock", "Win32Clock", "timeBeginPeriod() returns error");
	LARGE_INTEGER freq;
	if (!QueryPerformanceFrequency(&freq))
		throw Error(Error::FATAL, "Win32Clock", "Win32Clock", "Cannot create Timer");
	c = 1000.0f / freq.QuadPart;
}

double Win32Clock::getSeconds()
{
	LARGE_INTEGER count;
	if (!QueryPerformanceCounter(&count))
		throw Error(Error::FATAL, "Win32Clock", "getCount", "Cannot update timer");
	return c * count.QuadPart / 1000.0;
}

Win32Clock::~Win32Clock()
{
	timeEndPeriod(1);
}
